﻿using BigfootMVC.Helpers;
using DotNetNuke.Entities.Modules;

namespace BigfootMVC.DNN
{

    /// <summary>
    /// This is the class from which all dnn module controls need to inherit. It provides
    /// a series of methods and classes that give you easy access to the different helpers throughout the system
    /// It also provides aliased functions like RenderPartial which is part of the JSHelper for easy access.
    /// </summary>
    public abstract class BaseControl : PortalModuleBase
    {

        /// <summary>
        /// This funciton must be overridden by the inheritor in order to Init the application parameters
        /// </summary>
        public abstract DnnApp InitApp();

        /// <summary>
        /// Returns the application object for the current module
        /// </summary>
        public DnnApp App { get { return _app ?? (_app = InitApp()); } }
        private DnnApp _app;

        /// <summary>
        /// This field is set by the route.renderpartial to indicate to the base view that it is being rendered by it. 
        /// </summary>
        public bool IsRenderPartial;

        /// <summary>
        /// Creates a default route (home/index.ascx). But everything else is fully usable
        /// </summary>
        public RouteInfo Route { get { return _route ?? (_route = new RouteInfo(this, PortalId, App)); } }
        private RouteInfo _route;

       
        /// <summary>
        /// Inclues all view helper functions (i.e. RenderPartial, E, UrlP, Url, Html, JS, TB, Ajax, CreatePartialForm, SubmitPartialForm, RenderIf, Post values, etc.)
        /// </summary>
        public DnnViewHelper BF
        {
            get
            {
                return _viewHelper ?? (_viewHelper = new DnnViewHelper(Route, IsRenderPartial));
            }
        }
        private DnnViewHelper _viewHelper;


        // These are alias functions from BF used for convinience
        public string uid(string name) { return BF.uid(name); }
        public HtmlHelper Html { get { return BF.Html; } }
        public JSHelper JS { get { return BF.JS; } }
        public TagBuilder TB(string tagName) { return BF.TB(tagName); }
        public string E(string value) { return BF.E(value); }
        public string F(string format, params string[] values) { return BF.F(format, values); }
        public AjaxRequest Ajax(string url) { return BF.Ajax(url); }
        public UrlHelper Url { get { return BF.Url; } }
        public string UrlP(string paramName, string value) { return BF.UrlP(paramName, value); }
        public string UrlP(string paramName, int value) { return BF.UrlP(paramName, value); }
    }
}
